//
//  JCCashRecordController.m
//  Lead
//
//  Created by shouhan on 2018/7/13.
//  Copyright © 2018年 陈永发. All rights reserved.
//

#import "JCCashRecordController.h"
#import "JCCashRecordCell.h"
#import "CashRecordModel.h"

@interface JCCashRecordController ()<UITableViewDelegate,UITableViewDataSource>
{
    int pageindex;
}
@property (nonatomic, strong) UITableView * recordTableView;

@property (nonatomic, strong) NSMutableArray * dataArray;

@end

@implementation JCCashRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"交易记录";
    pageindex = 1;
    self.dataArray = [NSMutableArray array];
    
    [self getRequestRecord:YES];
    
    self.recordTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    
    self.recordTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JCCashRecordCell * cell = [JCCashRecordCell tableViewCellWithTableView:tableView];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return __I6Height(80);
}

- (UITableView *)recordTableView{
    if (!_recordTableView) {
        _recordTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-KNAVIGATIONBARHEIGHT) style:UITableViewStylePlain];
        _recordTableView.delegate = self;
        _recordTableView.dataSource = self;
        _recordTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _recordTableView.backgroundColor = [UIColor colorWithHexString:@"#F7F8FB"];

        [self.view addSubview:_recordTableView];
    }
    return _recordTableView;
}

-(void)refresh
{
    pageindex = 1;
    [self getRequestRecord:YES];
}
-(void)loadMore
{
    pageindex ++;
    [self getRequestRecord:NO];
}

- (void)getRequestRecord:(BOOL)isRemove{
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    
    [dic setValue:[NSString stringWithFormat:@"%d",pageindex] forKey:@"pageindex"];
    [dic setValue:@"10" forKey:@"count"];
    
    [PPNetworkHelper GET:Query_nocard_trade parameters:dic cache:NO encrypt:NO decode:NO responseCache:^(id responseCache) {
        
    } success:^(id result) {
        if ([result[@"success"] boolValue] == YES) {
            if (isRemove) {
                [self.dataArray removeAllObjects];
            }
            [self.dataArray addObjectsFromArray:[CashRecordModel mj_objectArrayWithKeyValuesArray:result[@"data"]]];
        }else{
            [self.view makeToast:result[@"data"] duration:2.f position:@"center"];
        }

        [self.recordTableView.mj_header endRefreshing];
        [self.recordTableView.mj_footer endRefreshing];
        [self.recordTableView reloadData];
    } failure:^(NSError *error) {
        [self.recordTableView.mj_header endRefreshing];
        [self.recordTableView.mj_footer endRefreshing];
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
